Repository navigation
fix(proxy): keep forwarded requests reachable so a client disconnect reaches the daemon - #5058
Conversation
…reaches the daemon Undici's Request follows its init signal only while the Request itself is reachable: it links the signal to the Request's own controller through a weak reference. The proxy handed each Request it built to a transport or to the authenticate hook and kept no reference, so the first garbage collection during an exchange cut the link. A client that disconnected after that, upstreamTimeoutMs and the five-second health bound no longer reached the transport or the hook, and the daemon kept running the request. Each Request the proxy builds is now kept in a WeakMap keyed by the object it serves: the Node adapter's request by its ServerResponse, the upstream, health and authenticate requests by the client request, and a custom transfer transport's request by the upstream one. An owner keeps every request built for it, so one client request handled twice at once keeps both. A listener on the followed signal is not used because Undici keeps that signal reachable for as long as the Request is, so the Request would never be collected. The garbage-collection cases move to daemon-proxy-cancellation.test.ts.
|
Thanks for the fix. The PR is ready at 8b0d1a8: the change looks correct, and CI is green with no conflicts. Not blocking, and fine to take or leave: the README's "relies on" list in the bring-your-own-transport section could say that the host keeps the Request reachable until the Response body ends, as the |
forwardedRequest builds the authenticate, health and upstream requests and keeps them with the client request in one step, so the three sites cannot drift. The Node adapter and custom transfer wrappers keep calling keepAliveWith, since they do not build the request.
The upstreamTimeoutMs case now uses a real 300 ms AbortSignal.timeout after the collection instead of a mocked signal. The mocked health-bound case restores its spy in onTestFinished, so a timeout there cannot leave AbortSignal.timeout mocked for the next case.
|
|
Thanks for the quick follow-up. The three non-blocking suggestions from the earlier review (#5058 (comment)) are now taken at fae9a52: the README bullet, the real 300 ms timer test, and the shared forwardedRequest helper replacing the three identical call sites. The code looks good and no new problems were found. All 15 checks pass or are skipped at this commit, and the earlier cancelled Body rules run was replaced by a passing run. No conflicts are known, and nothing blocks merge. Not verified here: the tests and the base/head mutants were not run locally, the Node 22.12 / Undici 6.21 behavior and how Bun or Hono hold the Request while sending, and the 90 s live |
Summary
Follow-up to #3256. After a garbage collection, a client leaving a request through
agent-device-proxyno longer cancelled it on the daemon.Undici's
Requestfollows its init signal only while theRequestitself is reachable. The proxy kept no reference to the upstream, health and custom transfer requests, or to the one it passes theauthenticatehook. After a collection, the client's abort,upstreamTimeoutMsand the health bound no longer reached the transport or the hook. The Node adapter dropped its own request once the response started.The default fetch loses the abort on Node 24.19 (Undici 7.29.0) but not on Undici 6.21 (the Node 22.12 floor). Custom transports and the hook lose it on both.
Each built request is kept in a
WeakMapkeyed by the client request, or the adapter'sServerResponse.DaemonProxy.handleand the README ask hosts to keeprequestreachable until the body ends.Before / after
pnpm exec vitest run --project unit-core packages/daemon/src/server/http/http-server-proxy.test.ts -t "after a garbage collection":agent-device openthrough a proxy callinggc()every 500 ms (--expose-gc): at head the daemon cancelled it at 90.13 s; at base it still ran 15 s after the client left.Validation
Head
fae9a52bd.pnpm exec vitest run --project unit-core packages/proxy/src packages/daemon/src/server/http/http-server-proxy.test.ts: 152 passed, three runs; 9 fail at base. Tests force collections viav8.setFlagsFromString('--expose-gc').pnpm check:affected --run: format, lint, typecheck, layering, fallow, build and vitest-related passed.pnpm check:daemon-wire-compat: protocol unchanged.